/**
 * Candidate number: 35834
 */
package tv.starfu.systems.level;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.EntityProcessingSystem;
import com.jme3.math.Vector3f;
import tv.starfu.components.Transform;
import tv.starfu.components.level.Compass;
import tv.starfu.components.level.KyberneteGrid;
import tv.starfu.components.level.GridPosition;
import tv.starfu.components.level.Path;


/**
 * 
 * @author nia radical
 */
public class KyberTileSystem extends EntityProcessingSystem {
    
    public boolean isProcessed;
    private ComponentMapper<GridPosition>   gridPositionMapper;
    private ComponentMapper<Path>           pathMapper;
    private ComponentMapper<Compass>        compassMapper;
    private ComponentMapper<Transform>      transformMapper;
    
    private Entity grid;
    private Transform gridTransform;
    private KyberneteGrid kyberGrid;
    
    private int tileSize;
    
    
    /* Combine GridPosition, Path, Compass and KyberGrid.Transform */
    public KyberTileSystem(int tileSize){
        super(GridPosition.class, Path.class, Compass.class,Transform.class);
        isProcessed = false;
        this.tileSize = tileSize;
    }
    
    @Override
    public void initialize() {
        gridPositionMapper      = new ComponentMapper<GridPosition>(GridPosition.class, world);
        pathMapper              = new ComponentMapper<Path>(Path.class, world);
        compassMapper           = new ComponentMapper<Compass>(Compass.class, world);
        transformMapper         = new ComponentMapper<Transform>(Transform.class, world);

    }

 
    /**
     * Create Path from Compass data
     */
    @Override
    protected void process(Entity e) {
                
        grid = world.getTagManager().getEntity("TILEGRID");
        gridTransform = grid.getComponent(Transform.class);
        kyberGrid = grid.getComponent(KyberneteGrid.class);
        
            Path path                = pathMapper.get(e);
            Transform transform      = transformMapper.get(e);
            Compass compass          = compassMapper.get(e);
            GridPosition coordinates = gridPositionMapper.get(e);
            
           // coordinates.x            = (int) transform.position.x;
           // coordinates.y            = (int) transform.position.z;
          
            if(!isProcessed) {
        // Create walkable tiles
        if(compass.NORTH) {
            path.cells[1] .turnOn();
            path.cells[2] .turnOn(); }
        
        if(compass.EAST) {
            path.cells[7] .turnOn();
            path.cells[11].turnOn(); }
        
        if(compass.SOUTH) {
            path.cells[13].turnOn();
            path.cells[14].turnOn(); }
        
        if(compass.WEST) {
            path.cells[4] .turnOn();
            path.cells[8] .turnOn(); }

            path.cells[5] .turnOn();
            path.cells[6] .turnOn();
            path.cells[9] .turnOn();
            path.cells[10].turnOn();
            isProcessed = true;
            }
            
    }
    
}
